import 'package:flutter/material.dart';
import 'dart:ui' as ui;

class PhotoPainter extends CustomPainter {
  final ui.Image _image;
  final double marginLeft;
  final double marginTop;
  final double newImageWidth;
  final double newImageHeight;

  final Paint _paint = new Paint()..isAntiAlias = true;

  PhotoPainter(this._image, this.marginLeft, this.marginTop, this.newImageWidth,
      this.newImageHeight);

  // size为屏幕尺寸
  @override
  void paint(Canvas canvas, Size size) {
    if (_image != null) {
      Rect fromRect = Rect.fromLTWH(
          0, 0, _image.width.toDouble(), _image.height.toDouble());
      Rect dstRect =
          Rect.fromLTWH(marginLeft, marginTop, newImageWidth, newImageHeight);
      // margin为负，超出屏幕的部分不绘制...玛德，回退的时候会看到超出屏幕的部分，如果用cupertino回退
      Rect rect = Rect.fromLTWH(0, 0, size.width, size.height);
      canvas.clipRect(rect);
      // 参数2：截取的图片区域，参数3：目标绘制区域
      canvas.drawImageRect(_image, fromRect, dstRect, _paint);
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    // print("shouldRepaint");
    return true;
  }
}
